package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.entity.*;
import com.zzyl.exception.BusinessException;
import com.zzyl.mapper.*;
import com.zzyl.service.RoomService;
import com.zzyl.vo.BedVo;
import com.zzyl.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {
    @Autowired
    private RoomTypeMapper roomTypeMapper;
    @Autowired
    private FloorMapper floorMapper;
    @Autowired
    private ElderMapper elderMapper;
    @Autowired
    private BedMapper bedMapper;
    @Autowired
    private CheckInConfigMapper checkInConfigMapper;
    @Override
    public void saveOrUpdateRoom(Room room) {
        // 1.根据typeName去房型表中查询
        LambdaQueryWrapper<RoomType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoomType::getName,room.getTypeName());
        RoomType roomType = roomTypeMapper.selectOne(wrapper);
        if (roomType ==null){
            throw new BusinessException("您选择的房型有误");
        }
        room.setTypeId(roomType.getId());
        // 2.保存
        if(room.getId()!=null){
            System.out.println(room.id);
            getBaseMapper().updateById(room);
        }else{
            getBaseMapper().insert(room);
        }
    }

    @Override
    public List<RoomVo> findByFloorId(Long floorId) {
        return getBaseMapper().findByFloorId(floorId);
    }

    @Override
    public RoomVo getRoomById(Long id) {
        Room room = getBaseMapper().selectById(id);
        RoomVo roomVo = BeanUtil.copyProperties(room, RoomVo.class);
        LambdaQueryWrapper<Bed> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Bed::getRoomId,id);
        List<Bed> beds = bedMapper.selectList(wrapper);
        int totalBeds = beds.size();
        int oocipiedBeds = (int) (beds.stream().filter(e->e.getBedStatus()==1).count());
        roomVo.setOccupiedBeds(oocipiedBeds);
        roomVo.setTotalBeds(totalBeds);
        if (totalBeds!=0 && oocipiedBeds!=0){
            roomVo.setOccupancyRate(oocipiedBeds*1.0 / totalBeds);
        }else {
            roomVo.setOccupancyRate(0.0);
        }
        List<BedVo> collect = beds.stream().map(e -> {
            BedVo bedVo = new BedVo();
            bedVo.setBedNumber(e.getBedNumber());
            LambdaQueryWrapper<Elder> elderLambdaQueryWrapper = new LambdaQueryWrapper<>();
            elderLambdaQueryWrapper.eq(Elder::getBedNumber, e.getBedNumber());
            if(e.getBedStatus() == 1){
                Elder elder = elderMapper.selectOne(elderLambdaQueryWrapper);
                if(elder!=null){
                    bedVo.setName(elder.getName());
                }
                LambdaQueryWrapper<CheckInConfig> checkInConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
                checkInConfigLambdaQueryWrapper.eq(CheckInConfig::getElderId, elder.getId());
                CheckInConfig checkInConfig = checkInConfigMapper.selectOne(checkInConfigLambdaQueryWrapper);
              if(checkInConfig!=null){
                  bedVo.setLname(checkInConfig.getNursingLevelName());
                  bedVo.getCheckInConfigVo().setCheckInStartTime(checkInConfig.getCheckInStartTime());
                  bedVo.getCheckInConfigVo().setCheckInStartTime(checkInConfig.getCheckInEndTime());
              }
            }
            return bedVo;
        }).collect(Collectors.toList());
        roomVo.setBedVoList(collect);

        return roomVo;
    }

}
